/**
 *  Copyright (C) 2012  Fernando Gil
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   The following additional terms will apply to the software provieded with
 *   this file.
 *      1. Preserve the original authors’ names.
 *      2. Documenting all contributions and specify the contributor's name,
 *         the date of the contribution and some email to future contact.
 */

package org.dss.gil.util;

import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;


/** 
 *  @author  Fernando Gil <lobho.gil@gmail.com>
 *  @since   September 2012
 *  @version 1.0
 */
public class XMLBlocks extends XMLManager{

    private static final Logger logger = Logger.getLogger(XMLBlocks.class);
    private ArrayList<Object[]> array_lines;
    private Object[] obj_row;
    private String tag_name;
    private String globalComments;
    
    public XMLBlocks() throws SAXException {
        super();
        this.array_lines = new ArrayList();
        this.tag_name="";
        this.globalComments="";
    }


    /** Detect when a opening tag is reade.
     *  In that moment we can detect that the Tag is Block
     * and when the tag is block, create a new Object[]
     * to keep the new row's elements.
     * @param uri
     * @param name
     * @param qName
     * @param atts
     */
    @Override
    public void startElement(String uri, String name,String qName, Attributes atts) {
        //logger.debug("Element: " + name);
        if(name.equals("block")){            //if the element is <block>
            obj_row = new Object [Globals.TOTAL_COLUMNS];   //Init the "row"
        }
        this.tag_name = name;  //Save the name of the tag
    }

    /**
     * When an closing tag is reade, save the Object[] created previously
     * in the array list.
     * @param uri
     * @param name
     * @param qName
     */
    @Override
    public void endElement(String uri, String name,String qName) {
        logger.debug("End Element: " + name);
        if(name.equals("block")){            //if the element is <block>
            this.array_lines.add(obj_row);   //Save the "row"
        }
        this.tag_name = "";
    }

    /** Method to obatain the String between <tag-name>HERE</tag-name>
     *  save thatString in the correct position in the Object[]
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if(this.tag_name.equals("globalcomments")){
            this.globalComments+= new String(ch, start, length)+"\n";
        }else if(this.tag_name.equals("active")){
            String value = new String(ch, start, length);
            this.obj_row[0]= Boolean.parseBoolean(value);
        }else if(this.tag_name.equals("label")){
            this.obj_row[1]= new String(ch, start, length);
        }else if(this.tag_name.equals("name")){
            this.obj_row[2]= new String(ch, start, length);
        }else if(this.tag_name.equals("values")){
            this.obj_row[3]= new String(ch, start, length);
        }else if(this.tag_name.equals("comment")){
            this.obj_row[4]= new String(ch, start, length);
        }
        tag_name = "";
    }

    /**
     *
     * @return The array with all the readed lines
     */
    public ArrayList getSimulation() {
        return this.array_lines;
    }

    public String getGlobalComments() {
        return this.globalComments;
    }

}
